using System;
using System.Web.UI.WebControls;
using TrainGame.Model;
using TrainGame.GlobalState;

namespace _1856.UI
{
	/// <summary>
	/// Summary description for BuyNewTrain.
	/// </summary>
	public partial class TradeInTrain : System.Web.UI.Page
	{
	
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void Page_Load(object sender, System.EventArgs e)
		{
			ErrorMsg.Text = "";
			
			FillTableWithTrainInfo();
		}


		private void FillTableWithTrainInfo()
		{
			Game	game = Global.getGame (this);;

			int				nQuantity4		= 0;
			int				nQuantity5		= 0;
			int				nQuantity6		= 0;
			bool			bEnabled;
			Button			button;
			TableCell		tableCell;
			PublicCompany	company;

			company = game.CurrentlyOperatingCompany;
			CompanyMoney.Text = company.Name + " has $" + company.Cash;
				
			foreach (Train train in company.Trains)
			{
				if (train.Name == "4")
					nQuantity4++;
				
				if (train.Name == "5")
					nQuantity5++;
				
				if (train.Name == "6")
					nQuantity6++;
			}

			// set info for 4 train
			tableCell		= (TableCell) TrainTable.FindControl("Quanity_4"); 
			tableCell.Text	= nQuantity4.ToString();
			
			tableCell		= (TableCell) TrainTable.FindControl("Buy_4"); 
			button			= new Button();
			button.Text		= "Trade";
			bEnabled		= nQuantity4 > 0;	// can trade once a 6 has been bought
			button.Enabled	= bEnabled;
			button.Click	+= new System.EventHandler(this.TradeTrain);
			button.CommandArgument = "4";
			tableCell.Controls.Add(button);
			
			// set info for 5 train
			tableCell		= (TableCell) TrainTable.FindControl("Quanity_5"); 
			tableCell.Text	= nQuantity5.ToString();
			
			tableCell		= (TableCell) TrainTable.FindControl("Buy_5"); 
			button			= new Button();
			button.Text		= "Trade";
			bEnabled		= nQuantity5 > 0;	// can trade once a 6 has been bought
			button.Enabled	= bEnabled;
			button.Click	+= new System.EventHandler(this.TradeTrain);
			button.CommandArgument = "5";
			tableCell.Controls.Add(button);
			
			// set info for 6 train
			tableCell		= (TableCell) TrainTable.FindControl("Quanity_6"); 
			tableCell.Text	= nQuantity6.ToString();
			
			tableCell		= (TableCell) TrainTable.FindControl("Buy_6"); 
			button			= new Button();
			button.Text		= "Trade";
			bEnabled		= nQuantity6 > 0;	// can trade once a 6 has been bought
			button.Enabled	= bEnabled;
			button.Click	+= new System.EventHandler(this.TradeTrain);
			button.CommandArgument = "6";
			tableCell.Controls.Add(button);
			
			// do only 8 or D but not both and hide the non-appropriate row and column
			if (game.MaxTrain8Train)
			{
				// hide the cells and row for the D train
				for (int i = 0; i < TrainTable.Rows.Count; i++)
					TrainTable.Rows[i].Cells[5].Visible = false;
			}
			else
			{
				// hide the cells and row for the 8 train
				for (int i = 0; i < TrainTable.Rows.Count; i++)
					TrainTable.Rows[i].Cells[4].Visible = false;
			}
		}
		

		private void TradeTrain(object sender, System.EventArgs e)
		{
			Game			game			= Global.getGame (this);;
			string			sTrain;
			string			sTradedTrain	= "Diesel";
			Button			button			= (Button) sender;
			PublicCompany	company;

			company = game.CurrentlyOperatingCompany;
			sTrain	= button.CommandArgument;

			if (game.MaxTrain8Train)
				sTradedTrain = "8";

			if (company != null)
			{
				string	sErrorMsg;
	
				if (company.TradeInTrain(sTrain, out sErrorMsg))
				{
					string	sMsg;

					sMsg = company.Name + " traded in a " + sTrain + " train for a " + sTradedTrain + " train";
					game.History.Add (sMsg);
					
					Response.Redirect("TradeInTrain.aspx");
				}
				else
				{
					ErrorMsg.Text = sErrorMsg;
				}
			}
		}

		protected void Back_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("BuyTrain.aspx");
		}

	}
}
